package com.chexiehui.jc.mvp.car;

import com.chexiehui.jc.Constants;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePage;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.entity.BrandListInfo;
import com.chexiehui.jc.entity.CarSeriesInfo;
import com.chexiehui.jc.entity.DictInfo;

import java.util.List;

/**
 * @Description 新车
 * @Auth SongJun
 * @Date 2020/05/21 00:18
 **/
public class CarGarageNewPresenter extends BasePresenter<CarGarageNewCovenant.View, CarGarageNewCovenant.Stores>
        implements CarGarageNewCovenant.Presenter {

    public CarGarageNewPresenter(CarGarageNewCovenant.View view) {
        attachView(view);
    }

    @Override
    public void getCruxCarList() {
        addSubscription(appStores.getCruxCarList("3", 1, 50),
                new ApiCallback<BaseModel<BasePage<CarSeriesInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<BasePage<CarSeriesInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().getItems() != null &&
                                    model.getData().getItems().size() > 0) {
                                mvpView.onGetCruxCarListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "没有找到任何数据");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetCruxCarListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void getFollowCarList() {
        addSubscription(appStores.getFollowCarList(1, 50),
                new ApiCallback<BaseModel<BasePage<CarSeriesInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<BasePage<CarSeriesInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().getItems() != null &&
                                    model.getData().getItems().size() > 0) {
                                mvpView.onGetFollowCarListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "没有找到任何数据");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetFollowCarListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void getCraGradeList() {
        addSubscription(appStores.getCraGradeList(""),
                new ApiCallback<BaseModel<List<DictInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<List<DictInfo>> model) {
                        if (model.getResultCode() == 0 && model.getData() != null && model.getData().size() > 0) {
                            mvpView.onGetCraGradeListSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetCraGradeListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void getBrandList() {
        addSubscription(appStores.getBrandList(""),
                new ApiCallback<BaseModel<List<BrandListInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<List<BrandListInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().size() > 0) {
                                mvpView.onGetBrandListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "暂无品牌信息");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetBrandListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void doFollowSeries(String seriesId) {
        addSubscription(appStores.doFollow(seriesId, "3"),
                new ApiCallback<BaseModel<String>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<String> model) {
                        if (model.getResultCode() == 0) {
                            model.setData(seriesId);
                            mvpView.onDoFollowSeriesSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onDoFollowSeriesFailure(new BaseModel<>(code, msg));
                    }
                });
    }

}